#include "course.h"
#include <time.h>

// 添加课程到链表
void add_course(Course **head, const char *id, const char *name, int credits, const char *properties, int selected_num, int max_students)
{
    Course *new_course = (Course *)malloc(sizeof(Course));
    if (new_course == NULL)
    {
        perror("Failed to allocate memory for new course");
        return;
    }

    strcpy(new_course->id, id);
    strcpy(new_course->name, name);
    new_course->credits = credits;
    strcpy(new_course->properties, properties);
    new_course->selected_num = selected_num;
    new_course->max_students = max_students;
    new_course->next = *head;
    *head = new_course;

    //添加课程信息到日志
    log_action("Add-Course", id);
}

// 删除课程
void delete_course(Course **head, const char *id)
{
    Course *current = *head, *prev = NULL;
    while (current != NULL && strcmp(current->id, id) != 0)
    {
        prev = current;
        current = current->next;
    }
    if (current == NULL)
    {
        printf("课程ID为 %s 的课程没有发现.\n", id);
        return;
    }
    if (prev == NULL)
    {
        *head = current->next;
    }
    else
    {
        prev->next = current->next;
    }
    free(current);

    printf("删除课程成功！\n");

    //记录删除课程日志
    log_action("Delete-Course", id);
}

// 返回学生指针
Course *get_course(Course **head, const char *id)
{
    Course *current = *head;
    while (current != NULL)
    {
        if (strcmp(current->id, id) == 0)
        {
            return current;
        }
        current = current->next;
    }
    return NULL;
}
// 更新课程信息
void update_course(Course **head, const char *id)
{
    Course *course = get_course(head, id);
    if (course == NULL)
    {
        printf("课程ID为 %s 的课程没有发现.\n", id);
        return;
    }

    char item;
    printf("是否要更新课程名称？ (y / n)\n");
    scanf(" %c", &item);
    if (item == 'y')
    {
        printf("请输入新的课程名称: ");
        scanf("%s", course->name);
    }
    printf("是否要更新课程学分？ (y / n)\n");
    scanf(" %c", &item);
    if (item == 'y')
    {
        printf("请输入新的课程学分: ");
        scanf("%d", &course->credits);
    }
    printf("是否要更新课程性质？ (y / n)\n");
    scanf(" %c", &item);
    if (item == 'y')
    {
        printf("请输入新的课程性质: ");
        scanf("%s", course->properties);
    }
    printf("是否要更新课程人数？ (y / n)\n");
    scanf(" %c", &item);
    if (item == 'y')
    {
        printf("请输入新的已选人数: ");
        scanf("%d", &course->selected_num);
    }
    scanf(" %c", &item);
    if (item == 'y')
    {
        printf("请输入新的人数上限: ");
        scanf("%d", &course->max_students);
    }

    printf("更新课程成功!\n");

    //记录更新课程日志
    log_action("Update-Course", id);
}

const Course *get_const_course(const Course *head, const char *id)
{
    const Course *current = head;
    while (current != NULL)
    {
        if (strcmp(current->id, id) == 0)
        {
            return current;
        }
        current = current->next;
    }
    return NULL;
}
// 查看课程详细信息
void view_course(const Course *head, const char *id)
{
    const Course *course = get_const_course(head, id);
    if (course == NULL)
    {
        printf("课程ID为 %s 的课程没有发现.\n", id);
        return;
    }

    printf("课程ID: %s\n", course->id);
    printf("课程名称: %s\n", course->name);
    printf("课程学分: %d\n", course->credits);
    printf("课程性质: %s\n", course->properties);
    printf("已选人数: %d\n", course->selected_num);
    printf("人数上限: %d\n", course->max_students);

    //记录查询课程日志
    log_action("View-Course", id);
}
// 列出所有课程信息
void list_courses(const Course *head)
{
    const Course *current = head;
    while (current != NULL)
    {
        printf("ID: %s, 姓名: %s, 课程学分: %d, 课程性质: %s, 已选人数: %d, 人数上限: %d\n", current->id, current->name, current->credits, current->properties, current->selected_num, current->max_students);

        current = current->next;
    }
}

// 从文件中加载课程信息
void load_courses_from_file(Course **head, const char *filename)
{
    FILE *file = fopen(filename, "r");
    if (file == NULL)
    {
        perror("Failed to open file");
        return;
    }

    char id[20], name[50], properties[50];
    int credits, selected_num, max_students;

    while (fscanf(file, "%s %s %d %s %d %d", id, name, &credits, properties, &selected_num, &max_students) == 6)
    {
        add_course(head, id, name, credits, properties, selected_num, max_students);
    }

    fclose(file);
}

// 将课程信息保存到文件
void save_courses_to_file(const Course *head, const char *filename)
{
    FILE *file = fopen(filename, "w");
    if (file == NULL)
    {
        perror("Failed to open file");
        return;
    }

    const Course *current = head;
    while (current != NULL)
    {
        fprintf(file, "%s %s %d %s %d %d\n", current->id, current->name, current->credits, current->properties, current->selected_num, current->max_students);
        current = current->next;
    }

    fclose(file);

    //记录保存课程到文件的日志
    log_action("Save-Courses", "Saved courses to file");
}
